xi<template>
  <div class="comment-item">
    <div class="comment-content">
      <div class="avatar-wrapper">
        <a-avatar :size="40" :src="comment.avatar">
          {{ !comment.avatar ? comment.userId.charAt(0).toUpperCase() : '' }}
        </a-avatar>
      </div>
      <div class="comment-main">
        <div class="comment-info">
          <div class="username">{{ comment.username || comment.userId }}</div>
          <div class="comment-text" v-html="renderContent(comment.content)"></div>
          <!-- 图片预览 -->
          <div v-if="comment.imageUrl" class="image-preview">
            <img :src="comment.imageUrl" @click="$emit('preview-image', comment.imageUrl)" />
          </div>
          <div class="comment-footer">
            <span class="time">{{ comment.createTime }}</span>
            <a class="reply-btn" @click="$emit('reply', comment)">回复</a>
          </div>
        </div>
        
        <!-- 子评论区域 -->
        <div v-if="comment.replyCount > 0" class="comment-replies">
          <div v-if="!isExpanded" class="expand-button">
            <a @click="toggleExpand">展开{{ comment.replyCount }}条回复</a>
          </div>
          <template v-else>
            <div class="replies-wrapper">
              <comment-item
                v-for="child in comment.children"
                :key="child.id"
                :comment="child"
                @reply="$emit('reply', $event)"
                @preview-image="$emit('preview-image', $event)"
              />
            </div>
            <div class="expand-button">
              <a @click="toggleExpand">收起回复</a>
            </div>
          </template>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { marked } from 'marked'
import DOMPurify from 'dompurify'
import hljs from 'highlight.js'
import 'highlight.js/styles/atom-one-dark.css'

// 配置 marked
marked.setOptions({
  highlight: function(code, lang) {
    if (lang && hljs.getLanguage(lang)) {
      try {
        return hljs.highlight(lang, code).value
      } catch (err) {}
    }
    return hljs.highlightAuto(code).value
  },
  breaks: true,
  gfm: true
})

export default {
  name: 'CommentItem',
  props: {
    comment: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      isExpanded: false
    }
  },
  methods: {
    renderContent(content) {
      if (!content) return ''
      const html = marked(content)
      return `<div class="markdown-content">${DOMPurify.sanitize(html)}</div>`
    },
    toggleExpand() {
      this.isExpanded = !this.isExpanded
    }
  }
}
</script>

<style scoped>
.comment-item {
  padding: 12px 16px;
}

.comment-content {
  display: flex;
  gap: 12px;
}

.avatar-wrapper :deep(.ant-avatar) {
  border-radius: 50%;
  border: 0.5px solid #eee;
}

.comment-main {
  flex: 1;
  min-width: 0;
}

.comment-info {
  background: #f8f8f8;
  border-radius: 8px;
  padding: 8px 12px;
  position: relative;
}

.comment-info::before {
  content: '';
  position: absolute;
  left: -6px;
  top: 12px;
  width: 12px;
  height: 12px;
  background: #f8f8f8;
  transform: rotate(45deg);
}

.username {
  font-size: 14px;
  font-weight: 500;
  color: #222;
  margin-bottom: 4px;
}

.comment-text {
  font-size: 15px;
  color: #222;
  line-height: 1.4;
  margin-bottom: 8px;
  word-break: break-all;
}

/* 代码块样式 */
.comment-text :deep(.markdown-content pre) {
  margin: 16px 0;
  padding: 16px;
  background-color: #282c34;
  border-radius: 6px;
  overflow: auto;
}

.comment-text :deep(.markdown-content pre code) {
  padding: 0;
  background: none;
  color: #abb2bf;
}

.comment-text :deep(.markdown-content code) {
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace;
  font-size: 14px;
  padding: 2px 6px;
  background: #f6f8fa;
  border-radius: 3px;
  color: #476582;
}

.comment-text :deep(.markdown-content pre code.hljs) {
  padding: 0;
  color: #abb2bf;
}

.comment-text :deep(.markdown-content pre code.hljs .hljs-keyword) {
  color: #c678dd;
}

.comment-text :deep(.markdown-content pre code.hljs .hljs-string) {
  color: #98c379;
}

.comment-text :deep(.markdown-content pre code.hljs .hljs-number) {
  color: #d19a66;
}

.comment-text :deep(.markdown-content pre code.hljs .hljs-function) {
  color: #61afef;
}

.comment-text :deep(.markdown-content pre code.hljs .hljs-comment) {
  color: #5c6370;
  font-style: italic;
}

.comment-text :deep(.markdown-content pre code.hljs .hljs-title) {
  color: #61afef;
}

.comment-text :deep(.markdown-content pre code.hljs .hljs-params) {
  color: #abb2bf;
}

.comment-text :deep(.markdown-content pre code.hljs .hljs-built_in) {
  color: #e6c07b;
}

.comment-text :deep(.markdown-content h1),
.comment-text :deep(.markdown-content h2),
.comment-text :deep(.markdown-content h3) {
  margin-top: 24px;
  margin-bottom: 16px;
  font-weight: 600;
  line-height: 1.25;
  border-bottom: 1px solid #eaecef;
  padding-bottom: 0.3em;
}

.comment-text :deep(.markdown-content h1) {
  font-size: 2em;
}

.comment-text :deep(.markdown-content h2) {
  font-size: 1.5em;
}

.comment-text :deep(.markdown-content h3) {
  font-size: 1.25em;
}

.comment-text :deep(.markdown-content p) {
  margin: 16px 0;
  line-height: 1.7;
}

.comment-text :deep(.markdown-content ul),
.comment-text :deep(.markdown-content ol) {
  padding-left: 2em;
  margin: 16px 0;
}

.comment-text :deep(.markdown-content li) {
  margin: 8px 0;
}

.comment-text :deep(.markdown-content blockquote) {
  margin: 16px 0;
  padding: 0 16px;
  color: #6a737d;
  border-left: 4px solid #dfe2e5;
}

.comment-text :deep(.markdown-content img) {
  max-width: 100%;
  border-radius: 4px;
  margin: 16px 0;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.comment-text :deep(.markdown-content table) {
  border-collapse: collapse;
  width: 100%;
  margin: 16px 0;
}

.comment-text :deep(.markdown-content th),
.comment-text :deep(.markdown-content td) {
  border: 1px solid #dfe2e5;
  padding: 8px 12px;
}

.comment-text :deep(.markdown-content th) {
  background-color: #f6f8fa;
}

.image-preview {
  margin: 8px 0;
}

.image-preview img {
  max-width: 200px;
  max-height: 200px;
  border-radius: 8px;
  cursor: pointer;
}

.comment-footer {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-top: 4px;
}

.time {
  font-size: 12px;
  color: #999;
}

.reply-btn {
  font-size: 13px;
  color: #999;
  cursor: pointer;
}

.reply-btn:hover {
  color: #666;
}

.comment-replies {
  margin-top: 8px;
  margin-left: 4px;
}

.replies-wrapper {
  transition: all 0.3s ease;
  margin-left: 24px;
  border-left: 2px solid #f1f1f1;
  padding-left: 12px;
}

.expand-button {
  padding: 8px 0;
  margin-left: 52px;
}

.expand-button a {
  font-size: 13px;
  color: #999;
  cursor: pointer;
}

.expand-button a:hover {
  color: #666;
}
</style>
